<template>
  <div>
    <el-row type="flex" class="mb10">
      <el-col>
        <el-input
          search
          enter-button
          placeholder="请输入项目名称"
          v-model="itemForm.title"
          style="width: 400px;"
        >
          <el-button type="primary" slot="append" icon="el-icon-search" @click="examSearchs"></el-button>
        </el-input>
      </el-col>
    </el-row>
    <el-divider style="top:0;" />
    <el-table border :columns="columns" :data="itemList" :loading="loading">
      <el-table-column label="Id"  #default="{row}" align="center">{{row.id}}</el-table-column>
      <el-table-column label="项目名称"  #default="{row}" align="center">{{row.title}}</el-table-column>
      <el-table-column label="是否开通"  #default="{row}" align="center">
        <el-switch
          v-model="row.isRelate"
          :value="row.isRelate"
          :active-value="1"
          :inactive-value="0"
          active-text="开通"
          inactive-text="关闭"
          @change="onchangeStatus(row)"
          size="large"
        ></el-switch>
      </el-table-column>

    </el-table>
    <div class="acea-row row-right page">
      <el-pagination
        :total="total"
        show-elevator
        show-total
        :current-page="itemForm.page"
        @current-change="pageChange"
        :page-size="itemForm.limit"
      />
    </div>
  </div>
</template>

<script>
import { getStaffRelateApi, saveStaffRelate } from '@/api/base';
export default {
  name: 'staffRelate',
  props: {
    staff_id: 0,
  },
  data() {
    return {
      open_model: false,
      columns: [
        {
          title: 'id',
          key: 'id',
          align: 'center',
        },
        {
          title: '项目名称',
          key: 'title',
          align: 'center',
        },
        {
          title: '是否开通',
          slot: 'is_relate',
          align: 'center',
        },
      ],
      itemList: [],
      loading: false,
      itemForm: {
        page: 1,
        limit: 10,
        staffId: 0,
        title: 0,
        title: '',
      },
      total: 0,
    };
  },
  mounted() {
    if (this.staff_id) {
      this.open_model = true;
      this.itemForm.staffId = this.staff_id;
      this.getList();
    }
  },
  methods: {
    getList() {
      this.loading = true;
      getStaffRelateApi(this.itemForm).then((res) => {
        this.itemList = res.list;
        this.total = res.total;
        this.loading = false;
      });
    },

    // 分页
    pageChange(index) {
      this.itemForm.page = index;
      this.getList();
    },
    // 关闭弹框
    cancel() {
      this.itemForm = {
        page: 1,
        limit: 10,
        place_id: 0,
      };
    },
    // 搜索方法
    examSearchs() {
      this.getList();
    },
    // 开通或关闭
    onchangeStatus(row) {
      console.log(row,'row')
      let data = {
        id: row.isRelate,
        itemId: row.id,
        staffId: this.staff_id,
      };
      saveStaffRelate(data).then(async (res) => {
        this.$message.success('操作成功');
      }).catch(err => {
        this.$message.error('操作失败')
      });
    },
  },
};
</script>

<style scoped lang="stylus"></style>